"use strict";
cc._RF.push(module, '3067fH2FPhLBocVX+5UDOdn', 'shouShi_database');
// scripts/res/shouShi_database.ts

"use strict";
/**
 *
 * */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CLICK_MIN_PIXEL = void 0;
var skill_map_pos_json_1 = require("./skill_map_pos_json");
exports.CLICK_MIN_PIXEL = 20;
var shouShiDataBase = /** @class */ (function () {
    function shouShiDataBase() {
    }
    shouShiDataBase.getCalcSkillIdOfMap = function (poss) {
        //
        console.log(this.getAllMap(), "this.getAllMap()");
        var matchArray = [];
        for (var _i = 0, _a = this.getAllMap(); _i < _a.length; _i++) {
            var map = _a[_i];
            var match = this.analysisMapAndPossMatch(map, poss);
            matchArray.push({ id: map.id, match: match });
        }
        //按匹配度排序
        matchArray.sort(function (left, right) {
            return right.match - left.match;
        });
        console.log('排序后', matchArray);
        return matchArray[0].id;
    };
    shouShiDataBase.getAllMap = function () {
        if (this.allMap == null || this.allMap.length <= 0) {
            this.initMap();
        }
        return this.allMap;
    };
    shouShiDataBase.getMapOfId = function (id) {
        for (var _i = 0, _a = this.getAllMap(); _i < _a.length; _i++) {
            var map = _a[_i];
            if (map.id == id) {
                return map;
            }
        }
        return null;
    };
    /**
     * 此处可提前调用来初始化 如不提前初始化，请勿在内部直接调用 this.allMap, 而是调用 this.getAllMap()来访问 this.allMap;
     * */
    shouShiDataBase.initMap = function () {
        var count = 0;
        this.allMap = [];
        for (var id in skill_map_pos_json_1.default) {
            //
            this.allMap.push({
                id: id,
                i: count++,
                name: '',
                pos: skill_map_pos_json_1.default[id]
            });
        }
    };
    /**
     * 对齐点集
     * @param poss 源点集
     * @param length 对齐长度
     * @return 目标点集
     * */
    shouShiDataBase.alignmentPointSet = function (poss, length) {
        var rtPoints = [];
        //
        var addPointCount = length - poss.length;
        var scalePoint = addPointCount / poss.length;
        //需要添加才能对齐
        if (addPointCount > 0) {
            var needAdd = 0;
            //最后一个不动
            for (var i = 0; i < poss.length - 1; i++) {
                var curPos = poss[i];
                var nextPos = poss[i + 1];
                //源坐标先放入
                rtPoints.push({ x: curPos.x, y: curPos.y });
                //
                needAdd += scalePoint;
                //至少需要添加一个
                if (needAdd > 1) {
                    //取整数
                    var IntNeedAdd = Math.floor(needAdd);
                    //坐标差
                    var posLess = { x: nextPos.x - curPos.x, y: nextPos.y - curPos.y };
                    //插入n个 就分n+1段
                    var splitNum = IntNeedAdd + 1;
                    var scaleNum = 1 / splitNum;
                    //每一段的坐标偏移
                    var posScaleAdd = { x: posLess.x * scaleNum, y: posLess.y * scaleNum };
                    //
                    for (var i_1 = 0; i_1 < IntNeedAdd; i_1++) {
                        var x = posScaleAdd.x + posScaleAdd.x * i_1;
                        var y = posScaleAdd.y + posScaleAdd.y * i_1;
                        rtPoints.push({ x: curPos.x + x, y: curPos.y + y });
                    }
                    //添加完成 去掉整数
                    needAdd -= IntNeedAdd;
                }
            }
        }
        else if (addPointCount < 0) { //减少
            var needLess = 0;
            var lessPointCount = -addPointCount;
            var lessScalePoint = lessPointCount / length;
            for (var i = 0; i < poss.length - lessScalePoint; i++) {
                var currPos = poss[i];
                needLess += lessScalePoint;
                rtPoints.push({ x: currPos.x, y: currPos.y });
                while (needLess > 1) {
                    //跳过
                    i++;
                    //
                    needLess--;
                }
            }
            var lastPoint = poss[poss.length - 1];
            rtPoints.push({ x: lastPoint.x, y: lastPoint.y });
        }
        else { //已经对齐了
            for (var _i = 0, poss_1 = poss; _i < poss_1.length; _i++) {
                var pot = poss_1[_i];
                rtPoints.push({ x: pot.x, y: pot.y });
            }
        }
        //
        return rtPoints;
    };
    shouShiDataBase.isClick = function (poss) {
        var maxDistance = 0;
        var firstPos = poss[0];
        for (var _i = 0, poss_2 = poss; _i < poss_2.length; _i++) {
            var pot = poss_2[_i];
            var distance = pot.sub(firstPos).mag();
            maxDistance = Math.max(distance, maxDistance);
        }
        return maxDistance <= exports.CLICK_MIN_PIXEL;
    };
    /**
     * 分析镜像和坐标集的匹配度 [0,1]
     * @param map 映射镜像
     * @param poss 有序坐标集合
     * @return 匹配度百分比 0:完全不匹配 1:完全匹配 0.5:50%匹配
     * */
    shouShiDataBase.analysisMapAndPossMatch = function (map, poss) {
        //
        var o1Poss = this.alignmentPointSet(poss, map.pos.length);
        //开始分析
        var currMatchCount = 0;
        var currMatchSum = 0;
        //
        for (var i = 0; i < o1Poss.length - 1 && i < map.pos.length - 1; i++) {
            var currSkillPos = map.pos[i];
            var currCommandPos = o1Poss[i];
            var nextSkillPos = map.pos[i + 1];
            var nextCommandPos = o1Poss[i + 1];
            var skillRelations = this.getTwoPointRelations(currSkillPos, nextSkillPos);
            var commandRelations = this.getTwoPointRelations(currCommandPos, nextCommandPos);
            // console.log(`${map.name}角度--------------------`);
            // console.log(`${map.name}镜像角度`, skillRelations, currSkillPos, nextSkillPos);
            // console.log(`${map.name}指令角度`, commandRelations, currCommandPos, nextCommandPos);
            var currMatch = this.getRelationsMatchNumber(skillRelations, commandRelations);
            // console.log(i, '匹配度', currMatch);
            currMatchSum += currMatch;
            currMatchCount++;
        }
        var averageMatch = currMatchSum / currMatchCount;
        // console.log('总匹配度', averageMatch);
        return averageMatch;
    };
    /**
     * 比较相对关系
     * @param relationsPot1 角度1
     * @param relationsPot2 角度2
     * */
    shouShiDataBase.getRelationsMatchNumber = function (relationsPot1, relationsPot2) {
        var absAngle = Math.abs(relationsPot2 - relationsPot1);
        if (absAngle > 180)
            absAngle = 360 - absAngle;
        var match = (180 - absAngle) / 180;
        return match;
    };
    /**
     * 获取两点相对关系 角度
     * @return 角度
     * */
    shouShiDataBase.getTwoPointRelations = function (firstPot, nextPot) {
        var lp = cc.v2(firstPot.x, firstPot.y);
        var rp = cc.v2(nextPot.x, nextPot.y);
        var pot = rp.sub(lp);
        pot = this.clearZero(pot);
        var potXY0 = cc.v2(Math.abs(pot.x), 0.00001);
        var radian = potXY0.signAngle(pot);
        // console.log('下角度', pot, radian);
        //
        return this.radianToAngle(radian);
    };
    shouShiDataBase.clearZero = function (v2) {
        if (v2.x == 0) {
            v2.x = 0.001;
        }
        if (v2.y == 0) {
            v2.y = 0.001;
        }
        return v2;
    };
    //弧度转角度
    shouShiDataBase.radianToAngle = function (radian) {
        return radian * 180 / Math.PI;
    };
    //角度转弧度
    shouShiDataBase.angleTorDian = function (angle) {
        return angle * Math.PI / 180;
    };
    shouShiDataBase.allMap = [];
    return shouShiDataBase;
}());
exports.default = shouShiDataBase;

cc._RF.pop();